package com.eastday.dao.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.jdom.JDOMException;

import com.eastday.dao.impl.ConnectionBuilderDao;
import com.eastday.dao.UpdateNewsPageNoDao;
import com.eastday.util.ParseConfigFactory;

/**
 * 
 * @author dingxs
 *
 */
public class UpdateNewsPageNoOracleDaoImpl extends ConnectionBuilderDao implements UpdateNewsPageNoDao {
	
	public UpdateNewsPageNoOracleDaoImpl(){
		super();
		try {
			this.conntoOracleDb();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
	}

	@Override
	public void updateNewsPageNo(String url) {
		// TODO Auto-generated method stub
		try
		{
			String sqlStr = "SELECT count(news_id) FROM t_news_import WHERE pagesource=? and pageno=0";
			PreparedStatement psforSelect = conn.prepareStatement(sqlStr);
			psforSelect.setString(1, url);
			ResultSet rs = psforSelect.executeQuery();
			rs.next();
			int rowcount = rs.getInt(1);
			int pagesize = ParseConfigFactory.sysConfigModel.getExportPageSize();
			if(rowcount > 2*pagesize)
			{
				sqlStr = "SELECT MAX(pageno) FROM t_news_import WHERE pagesource=?";
				rs = psforSelect.executeQuery(sqlStr);
				rs.next();
				int maxPageCount = rs.getInt(1);
				psforSelect.close();
				maxPageCount++;
				sqlStr = "UPDATE t_news_import SET pageno=? "+
						 "WHERE t_news_import.news_id IN " +
						 "(SELECT news_id FROM " +
						 "(SELECT A.*,ROWNUM rn FROM " +
						 "(SELECT * FROM t_news_import WHERE pagesource=? and pageno=0 ORDER BY timeinfo) " +
						 "A WHERE ROWNUM<=?) WHERE rn >=1)";
				//System.out.println(sqlStr);
				PreparedStatement psforUpdate = conn.prepareStatement(sqlStr);
				psforUpdate.setInt(1, maxPageCount);
				psforUpdate.setString(2, url);
				psforUpdate.setInt(3, pagesize);
				psforUpdate.executeUpdate();
				
			}
			
		}
		catch(SQLException e)
		{
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		catch(Exception e)
		{
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		finally
		{
			this.closeConnection();
		}
	}

	@Override
	public void updateNewsPageNo() {
		// TODO Auto-generated method stub
		try {
			String sqlStr = "SELECT count(news_id) FROM t_news_import WHERE pageno=0";
			PreparedStatement psforSelect = conn.prepareStatement(sqlStr);
			ResultSet rs = psforSelect.executeQuery();
			rs.next();
			int rowcount = rs.getInt(1);
			int pagesize = ParseConfigFactory.sysConfigModel.getExportPageSize();
			if(rowcount > 2*pagesize)
			{
				sqlStr = "SELECT MAX(pageno) FROM  t_news_import";
				rs = psforSelect.executeQuery(sqlStr);
				rs.next();
				int maxPageCount = rs.getInt(1);
				psforSelect.close();
				maxPageCount++;
				sqlStr = "UPDATE t_news_import SET pageno=? " +
						"WHERE t_news_import.news_id IN " +
						"(SELECT news_id FROM " +
						"(SELECT A.*,ROWNUM rn FROM " +
						"(SELECT * FROM t_news_import WHERE pageno=0 ORDER BY timeinfo) " +
						"A WHERE ROWNUM<?) WHERE rn>=1)";
				PreparedStatement psforUpdate = conn.prepareStatement(sqlStr);
				psforUpdate.setInt(1, maxPageCount);
				psforUpdate.setInt(2, pagesize);
				psforUpdate.executeUpdate();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		} catch(Exception e) {
			this.exceptionHandler.writeToLog(e.getMessage());
		}
		finally
		{
			this.closeConnection();
		}
	}
	
	
	
}
